//
//  YQLTabBarController.swift
//  YQL_WeiBo
//
//  Created by apple on 2016/11/12.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

class YQLTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        addChildViewControllers()
        
        //使用KVC替换tabBarVC的tabBar为自定义的YQLTabBar
        let tabBar = YQLTabBar()
        self.setValue(tabBar, forKey: "tabBar")
        //tabBar的闭包回调
        tabBar.completeClosure = {
            
            print(#function,"tabBar的闭包回调")
        }
        
    }


    
    private func addChildViewControllers() {
        
        addchildViewControll(controller: YQLHomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addchildViewControll(controller: YQLMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addchildViewControll(controller: YQLDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addchildViewControll(controller: YQLProfileTableViewController(), title: "我", imageName: "tabbar_profile")

    }
    
    
    private func addchildViewControll(controller: UIViewController, title: String, imageName: String) {
        
        //设置标题
        controller.title = title
        //设置图片
        controller.tabBarItem.image = UIImage(named: imageName)
        controller.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        //设置文字颜色
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orange], for: .selected)
        //设置文字大小,文字大小只能设置默认状态才有效
        controller.tabBarItem.setTitleTextAttributes([NSFontAttributeName : UIFont.systemFont(ofSize: 10)], for: .normal)
        //设置文字和图片的距离
        controller.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -3)
        //设置提示标签,标签颜色
        controller.tabBarItem.badgeValue = "999"
        controller.tabBarItem.badgeColor = #colorLiteral(red: 0.4666666687, green: 0.7647058964, blue: 0.2666666806, alpha: 1)
        //取消标签就设置为nil
        //controller.tabBarItem.badgeValue = nil
        //设置图片偏移,此处设置偏移为0
        controller.tabBarItem.imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        
        //创建navigationVC,并将Controller设置为根控制器
        let navController = YQLNavigationController(rootViewController: controller)
        
        //设置navigationVC为tabBarVC的子控制器
        addChildViewController(navController)
        
    }
    
    

}
